package com.hwadee.teachingprocessmanagementsystem.exception;

import com.hwadee.teachingprocessmanagementsystem.util.ColorLoggerUtils;
import com.hwadee.teachingprocessmanagementsystem.util.result.DataResult;
import com.hwadee.teachingprocessmanagementsystem.util.result.code.Code;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: 全局异常处理
 * @Author: Jukomu
 * @Package: com.hwadee.teachingprocessmanagementsystem.exception
 * @Project: SpringBoot-demo
 * @name: GlobalExceptionHandler
 * @Date: 2024/7/5-1:29
 * @Filename: GlobalExceptionHandler
 */
@ControllerAdvice("com.hwadee.teachingprocessmanagementsystem")
public class GlobalExceptionHandler {
    private ColorLoggerUtils logger = new ColorLoggerUtils(GlobalExceptionHandler.class);
    /**
     * 出现异常返回错误提示, 并且回滚事务
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    @ResponseBody
    public DataResult exceptionHandler(Exception e){
        logger.error("抓到异常:"+e.getMessage());
        e.printStackTrace();
        return DataResult.errByErrCode(Code.ERROR);
    }
}
